﻿using System.Collections.Generic;

namespace Jayden.Dll.Web.Events
{
	public class HttpEventHandlerManager : Dictionary<string, HttpEventHandler>
	{
		public T GetHandler<T>(bool createIfNotExists = true) where T : HttpEventHandler, new()
		{
			T handler = new T();
			if(createIfNotExists)
				return GetHandler(handler) as T;
			return GetHandler(handler.Id, null) as T;
		}
		public HttpEventHandler GetHandler(HttpEventHandler handler) { return GetHandler(handler.Id, handler); }
		public HttpEventHandler GetHandler(string id, HttpEventHandler defaultValue = null)
		{
			HttpEventHandler handler = null;
			if (!TryGetValue(id, out handler))
			{
				handler = defaultValue;
				Add(id, handler);
			}
			return handler;
		}

		public void ProcessBinRequest(HttpHandlerContext context)
		{
			foreach (KeyValuePair<string, HttpEventHandler> pair in this)
				pair.Value.ProcessBinRequest(context);
		}
		public void ProcessHtmlRequest(HttpHandlerContext context)
		{
			foreach (KeyValuePair<string, HttpEventHandler> pair in this)
				pair.Value.ProcessHtmlRequest(context);
		}
		public void ProcessJsonRequest(HttpHandlerContext context)
		{
			foreach (KeyValuePair<string, HttpEventHandler> pair in this)
				pair.Value.ProcessJsonRequest(context);
		}
		public void ProcessJsRequest(HttpHandlerContext context)
		{
			foreach (KeyValuePair<string, HttpEventHandler> pair in this)
				pair.Value.ProcessJsRequest(context);
		}
		public void ProcessCssRequest(HttpHandlerContext context)
		{
			foreach (KeyValuePair<string, HttpEventHandler> pair in this)
				pair.Value.ProcessCssRequest(context);
		}
	}
}
